package lumis.service.wsrp.consumer;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.xml.ws.Holder;

import lumis.portal.PortalException;

import com.sun.portal.wsrp.common.stubs.v2.ModelDescription;
import com.sun.portal.wsrp.common.stubs.v2.PortletDescription;
import com.sun.portal.wsrp.common.stubs.v2.Property;
import com.sun.portal.wsrp.common.stubs.v2.RegistrationContext;
import com.sun.portal.wsrp.common.stubs.v2.WSRPV2PortletManagementPortType;
import com.sun.portal.wsrp.consumer.common.RemoteServiceStubManagerFactory;
import com.sun.portal.wsrp.consumer.common.WSRPConsumerException;

/**
 * Remote portlet manager implementation for version 2.
 *
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
public class RemotePortletManagementManagerV2 implements RemotePortletManagementManager
{
	private URL producerURL;
	private RegistrationContext registrationContext;
	
	private WSRPV2PortletManagementPortType portletManagementPortType;
	
	public RemotePortletManagementManagerV2(URL producerURL, RegistrationContext registrationContext)
	{
		this.producerURL = producerURL;
		this.registrationContext = registrationContext;
	}
	
	private WSRPV2PortletManagementPortType getPortletManagementImplementation() throws WSRPConsumerException
	{
		if (this.portletManagementPortType == null)
		{
			this.portletManagementPortType = RemoteServiceStubManagerFactory
												.getRemoteServiceStubManager()
													.getV2PortletManagementPortType(producerURL.toString());
		}
		return this.portletManagementPortType; 
	}

	public String getPortletName(String portletHandle) throws PortalException
	{
		PortletDescription portletDescription = getPortletDescription(portletHandle);
		if (portletDescription.getDisplayName() != null)
		{
			return portletDescription.getDisplayName().getValue();
		}
		else
		{
			return null;
		}		
	}

	public String getDescriptionFromPortlet(String portletHandle) throws PortalException
	{
		PortletDescription portletDescription = getPortletDescription(portletHandle);
		
		if (portletDescription.getDescription() != null)
		{
			return portletDescription.getDescription().getValue();
		}
		else
		{
			return null;
		}		
	}
	
	public PortletDescription getPortletDescription(String portletHandle)
									throws PortalException
	{
		try
		{
			RemotePortletHelperV2 helper = new RemotePortletHelperV2(portletHandle, registrationContext);
			
			WSRPV2PortletManagementPortType type = getPortletManagementImplementation();
			
			Holder<PortletDescription> portletDescriptionHolder = helper.getEmptyPortletDescriptionHolderV2();
			
			type.getPortletDescription(
							helper.getRegistrationContextV2(),
							helper.getPortletContextV2(),
							helper.getEmptyUserContextV2(),
							new ArrayList<String>(),
							portletDescriptionHolder,
							helper.getEmptyResourceListHolderV2(),
							helper.getEmptyExtensionListHolderV2());
			
			return portletDescriptionHolder.value;			
			
		}
		catch (Exception e)
		{
			throw new PortalException("STR_WSRP_NOT_OBTAIN_PORTLET_DESCRIPTION;" + portletHandle , e);
		}
	}
	
	public String clonePortlet(
					String portletHandle)
				throws PortalException
	{
		try
		{
			RemotePortletHelperV2 helper = new RemotePortletHelperV2(portletHandle, registrationContext);
			
			Holder<String> phh = helper.getEmptyPortletHandleHolder();
			
			WSRPV2PortletManagementPortType type = getPortletManagementImplementation();			
			type.clonePortlet(
					helper.getRegistrationContextV2(),
					helper.getPortletContextV2(),
					helper.getEmptyUserContextV2(),
					helper.getLifetimeV2(),
					phh,
					helper.getEmptyPortletStateHolder(),
					helper.getEmptyLifetimeHolderV2(),
					helper.getEmptyExtensionListHolderV2());			
			
			return phh.value;			
			
		}
		catch (Exception e)
		{
			throw new PortalException("STR_WSRP_NOT_CLONE_PORTLET;" + portletHandle , e);
		}		
	}

	public Properties getPortletProperties(
							String portletHandle)
						throws PortalException
	{
		try
		{
			RemotePortletHelperV2 helper = new RemotePortletHelperV2(portletHandle, registrationContext);
			
			Holder<List<Property>> v2Properties = helper.getEmptyPropertiesHolderV2();
			
			WSRPV2PortletManagementPortType type = getPortletManagementImplementation();			
			type.getPortletProperties(
						helper.getRegistrationContextV2(),
						helper.getPortletContextV2(),
						helper.getEmptyUserContextV2(),
						new ArrayList<String>(),
						v2Properties,
						helper.getEmptyResetPropertyListHolderV2(),
						helper.getEmptyExtensionListHolderV2());	
			
			Properties properties = RemotePortletHelperV2.toProperties(v2Properties);
			
			return properties;
		}
		catch (Exception e)
		{
			throw new PortalException("STR_WSRP_NOT_OBTAIN_PORTLET_PROPERTIES"  , e);
		}
	}

	public String getPortletPropertyDescription(
					String portletHandle,
					String propertyName)
				throws PortalException
	{	
		try
		{
			RemotePortletHelperV2 helper = new RemotePortletHelperV2(portletHandle, registrationContext);
			
			Holder<ModelDescription> modelDescription = helper.getEmptyModelDescriptionHolderV2();
			
			WSRPV2PortletManagementPortType type = getPortletManagementImplementation();
			type.getPortletPropertyDescription(
							helper.getRegistrationContextV2(),
							helper.getPortletContextV2(),
							helper.getEmptyUserContextV2(),
							new ArrayList<String>(),
							modelDescription,
							helper.getEmptyResourceListHolderV2(),
							helper.getEmptyExtensionListHolderV2());
			
			return RemotePortletHelperV2.toPropertyDescription(modelDescription, propertyName);
		}
		catch (Exception e)
		{
			throw new PortalException("STR_WSRP_NOT_OBTAIN_PROPERTY_FOR_PORTLET;" + propertyName + ";" + portletHandle , e);			
		}
	}

	public void removeClone(
					String portletHandle)
				throws PortalException
	{
		try
		{
			RemotePortletHelperV2 helper = new RemotePortletHelperV2(portletHandle, registrationContext);
			
			List<String> portletHandleList = new ArrayList<String>();
			portletHandleList.add(portletHandle);
			
			WSRPV2PortletManagementPortType type = getPortletManagementImplementation();
			type.destroyPortlets(
					helper.getRegistrationContextV2(),
					portletHandleList,
					helper.getEmptyUserContextV2(),
					helper.getEmptyFailedPortletsListHolderV2(),
					helper.getEmptyExtensionListHolderV2());
			
		}
		catch (Exception e)
		{
			throw new PortalException("STR_WSRP_NOT_REMOVE_CLONE;" + portletHandle, e);			
		}
		
	}

	public void setPortletProperties(
					String portletHandle,
					Properties properties)
				throws PortalException
	{
		RemotePortletHelperV2 helper = new RemotePortletHelperV2(portletHandle, registrationContext);
		
		try
		{
			WSRPV2PortletManagementPortType type = getPortletManagementImplementation();
			
			type.setPortletProperties(
						helper.getRegistrationContextV2(),
						helper.getPortletContextV2(),
						helper.getEmptyUserContextV2(),
						RemotePortletHelperV2.toPropertyListV2(properties),
						helper.getEmptyPortletHandleHolder(),
						helper.getEmptyPortletStateHolder(),
						helper.getEmptyLifetimeHolderV2(),
						helper.getEmptyExtensionListHolderV2());
		}
		catch (Exception e)
		{
			throw new PortalException("STR_WSRP_NOT_CHANGE_PROPERTIES;" + portletHandle , e);
		}	
	}
}